package com.wg.springboot.jdk;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ComputeIfAbsent {
    public static void main(String[] args) {
        HashMap<Integer, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put(1,2);
//        jdk8以前
        Object o = objectObjectHashMap.get(1);
        if (o==null){  //为空从新放一个
            Object o1 = new Object();
            objectObjectHashMap.put(1,o1);
        }
//        jdk8以后
//        objectObjectHashMap.computeIfAbsent(4,k->new ArrayList<>());
//        把上面合并一行  如果key等于4不存在就添加一个key等于4的键值对，value是第二个参数，是一个函数式接口，
//        接口里面的类型必须是返回的类型的子类或者同类,上面是object。public V computeIfAbsent(K key,
//                             Function<? super K, ? extends V> mappingFunction) {}这个没理解到wg? super K,
        Object o1 = objectObjectHashMap.computeIfAbsent(4, k -> new Integer(5));
        objectObjectHashMap.computeIfAbsent(4,k->new Integer(6));//执行到这里已经有4了
        objectObjectHashMap.computeIfAbsent(4,p->new Object());
        System.out.println(objectObjectHashMap);
//   返回值是map的value
        Object o2 = objectObjectHashMap.computeIfAbsent(4, p -> new ArrayList<String>());
        HashMap<String,List<String>> objectObjectHashMap1 = new HashMap<>();
        List<String> list2 = objectObjectHashMap1.computeIfAbsent("",k -> new ArrayList<String>());
    }
    @Test
    public void a(){}
}
